package com.view;

import android.content.Context;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.zhy.android.percent.support.PercentFrameLayout;

/**
 * Created by Administrator on 2016/7/6.
 */
public class SwipeItemLayout extends PercentFrameLayout {

    private int rightHideViewWidth ;
    private boolean isFirstAddView = true; //首次添加View
    private View slideView; //滑动的布局
    private View rightHideView; //隐藏在右侧底部的布局
    private SwipeListener swipeListener;
    private ViewDragHelper viewDragHelper;
    private boolean isOpened = false;
    public SwipeItemLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initSwipeItemLayout();
    }

    private void initSwipeItemLayout() {
        viewDragHelper = ViewDragHelper.create(this, new ViewDragHelper.Callback() {
            @Override
            public boolean tryCaptureView(View child, int pointerId) {
                return slideView == child;
            }

            @Override
            public int clampViewPositionHorizontal(View child, int left, int dx) {
                if (left >= 0) {
                    return 0;
                }
                if (left <= -rightHideViewWidth) {
                    return -rightHideViewWidth;
                }
                if (dx < 0) {
                    if(rightHideView.getVisibility()!=View.VISIBLE){
                        rightHideView.setVisibility(View.VISIBLE);
                    }
                    if (swipeListener != null) {
                        swipeListener.onStartSwipeOpen();
                    }
                }
                return left;
            }

            @Override
            public void onViewDragStateChanged(int state) {
                super.onViewDragStateChanged(state);
                switch (state) {
                    case ViewDragHelper.STATE_DRAGGING: //手指拖动
                        isOpened = false;
                        if(swipeListener!=null){
                            swipeListener.onSwiping();
                        }
                        break;
                    case ViewDragHelper.STATE_SETTLING: //手指抬起后View滑动
                        isOpened = false;
                        break;
                    case ViewDragHelper.STATE_IDLE: //滑动完成
                        if(slideView.getLeft()>=0){
                           rightHideView.setVisibility(View.GONE);
                            isOpened = false;
                            if(swipeListener!=null){
                                swipeListener.onSwipeCloseComplete();
                            }
                        }else{
                            isOpened = true;
                            if(swipeListener!=null){
                                swipeListener.onSwipeOpenComplete();
                            }
                        }
                        break;
                }
            }

            @Override
            public void onViewReleased(View releasedChild, float xvel, float yvel) {
                super.onViewReleased(releasedChild, xvel, yvel);
                if (slideView.getLeft() >= -rightHideViewWidth / 2) {
                    viewDragHelper.smoothSlideViewTo(slideView, 0, 0);
                    ViewCompat.postInvalidateOnAnimation(SwipeItemLayout.this);
                } else {
                    viewDragHelper.smoothSlideViewTo(slideView, -rightHideViewWidth, 0);
                    ViewCompat.postInvalidateOnAnimation(SwipeItemLayout.this);
                }
            }


        });
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        if (isFirstAddView) {
            if (getChildCount() != 2) {
                throw new IllegalStateException(SwipeItemLayout.class.getSimpleName() + "必须有且只有两个子布局");
            }
            slideView = getChildAt(1);
            rightHideView = getChildAt(0);
            rightHideViewWidth = rightHideView.getMeasuredWidth();
//            LogUtil.i("test","width:"+rightHideView.getMeasuredWidth());
            rightHideView.setVisibility(View.GONE);
            isFirstAddView = false;
        }
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = MotionEventCompat.getActionMasked(ev);
        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
            viewDragHelper.cancel();
            return false;
        }
        return viewDragHelper.shouldInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        viewDragHelper.processTouchEvent(event);
        return true;
    }

    //滑动关闭
    public void slideToClose() {
        if(isOpened){
            viewDragHelper.smoothSlideViewTo(slideView, 0, 0);
            ViewCompat.postInvalidateOnAnimation(SwipeItemLayout.this);
        }
    }

    public void setSwipeListener(SwipeListener swipeListener) {
        this.swipeListener = swipeListener;
    }

    @Override
    public void computeScroll() {
        if (viewDragHelper.continueSettling(true)) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }


    public interface SwipeListener {
        void onSwiping();
        void onStartSwipeOpen();
        void onSwipeOpenComplete();
        void onSwipeCloseComplete();
    }
}
